﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Lucene.Net.Index;
using Lucene.Net.Analysis.PanGu;
using System.IO;
using Lucene.Net.Store;

public partial class ct_manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }

        string username = Session["USERNAME"].ToString();
        //先获取enterid用enterid在info_ct中查询词条绑定到gridview，用多表把
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand com_all = sc.CreateCommand();
        com_all.CommandText = "select CT_ID,CT_NAME,CT_CONTENT,CT_IMAGE from INFO_CT where ENTER_ID=@eid1";
        com_all.Parameters.AddWithValue("@name",username);
        com_all.Parameters.AddWithValue("@eid1",Session["ENTERID"].ToString());
        SqlDataReader reader = com_all.ExecuteReader();
        GridView1.DataSource = reader;
        GridView1.DataBind();
        sc.Close();

    }
    protected void delete(object sender, GridViewDeleteEventArgs e)
    {
        if (Session["IDENTITY"].Equals("employee") == true)
        {
            Response.Redirect("error_auth.aspx", true);
        }
        String id = GridView1.Rows[e.RowIndex].Cells[0].Text;
        
        SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc1.Open();
        String sql = "delete from INFO_CT where CT_ID =@id";
        SqlCommand com = new SqlCommand(sql, sc1);
        com.Parameters.AddWithValue("@id", id);
        while (Application["sqllock"] == "lock")
        {//检测是否已被锁住
            System.Threading.Thread.Sleep(2000);
        }
        Application["sqllock"] = "lock";//上锁          
        try
        {
            com.ExecuteNonQuery();
        }
        catch (Exception ex_sql)
        {
            Application["sqllock"] = "unlock";//解锁
            Page.RegisterStartupScript("ss", "<script>alert('" + ex_sql.StackTrace + ex_sql.Message + "')</script>");
        }
        finally
        {
            Application["sqllock"] = "unlock";//解锁
        }
       
        string enterid = Session["ENTERID"].ToString();
        sc1.Close();  
        //不仅从数据库删除，索引里也删除
        try
        {
            IndexReader reader = IndexReader.Open(Server.MapPath("index/index_" + enterid));
            Term term = new Term("pid", id);
            reader.DeleteDocuments(term);

            reader.Close();
        }
        catch (Exception ee)
        {
            Page.RegisterStartupScript("ss", "<script>alert('"+ee.StackTrace+ee.Message+"')</script>");
            return;
        }
        string savePath = "~/product_image/" + id + "_image.jpg";//这里是你的相对路径
        savePath = Server.MapPath(savePath);
        if (System.IO.File.Exists(savePath))//先判断文件是否存在，再执行操作
            System.IO.File.Delete(savePath);
        Response.Redirect(Request.Url.ToString());
      
    }
 
}